<?php
namespace BP\En;
class EntityDBAccess {
	public static function Delete ($en) // [Entity en]
	{
		if (($en->getEnMap()->getEnType() == EnType::$View))
		{
			return 0;
		}
		switch ($en->getEnMap()->getEnDBUrl()->getDBUrlType()) {
			case $AppCenterDSN:
				return DBAccess::RunSQL($en->getSQLCash()->Delete, SqlBuilder::GenerParasPK($en));
			default:
				throw new \RuntimeException("@没有设置类型�?");
		}
	}
	public static function Update ($en, $keys) // [Entity en, String[] keys]
	{
		if (($en->getEnMap()->getEnType() == EnType::$View))
		{
			return 0;
		}
		$paras = SqlBuilder::GenerParas($en, $keys);
		$sql = $en->getSQLCash()->GetUpdateSQL($en, $keys);
		try 
		{
			switch ($en->getEnMap()->getEnDBUrl()->getDBUrlType()) {
				case $AppCenterDSN:
					switch ($SystemConfig->getAppCenterDBType()) {
						case $MSSQL:
						case $Oracle:
						case $MySQL:
						case $PostgreSQL:
							return DBAccess::RunSQL($sql, $paras);
						case $Informix:
			/* match: Entity_aString */
							return DBAccess::RunSQL($en->getSQLCash()->GetUpdateSQL($en, $keys), SqlBuilder->GenerParas_Update_Informix_Entity_aString($en, $keys));
						case $Access:
							return DBAccess::RunSQL(SqlBuilder::UpdateOfMSAccess($en, $keys));
						default:
							if (($keys != NULL))
							{
								$ps = new Paras();
								$myps = SqlBuilder::GenerParas($en, $keys);
								foreach ($myps as $p) 								{
									foreach ($keys as $s) 									{
										if ($p->ParaName->equals($s))
										{
											$ps->Add($p);
											break;
										}
									}
								}
								return DBAccess::RunSQL($en->getSQLCash()->GetUpdateSQL($en, $keys), $ps);
							}
							return DBAccess::RunSQL($en->getSQLCash()->GetUpdateSQL($en, $keys), SqlBuilder::GenerParas($en, $keys));
					}
				default:
					throw new \RuntimeException("@没有设置类型�?");
			}
		}
		catch (\RuntimeException $ex)
		{
			if (SystemConfig::getIsDebug())
			{
				$en->CheckPhysicsTable();
			}
			throw $ex;
		}
	}
	public static function RetrieveV2 ($en, $sql, $paras) // [Entity en, String sql, Paras paras]
	{
		try 
		{
			$dt = new DataTable();
			switch ($en->getEnMap()->getEnDBUrl()->getDBUrlType()) {
				case $AppCenterDSN:
					$dt = DBAccess->RunSQLReturnTable($sql, $paras);
					break;
				default:
					throw new \RuntimeException("@没有设置DB类型�?");
			}
			if (($dt->Rows->size() == 0))
			{
				return 0;
			}
			$attrs = $en->getEnMap()->getAttrs();
			EntityDBAccess::fullDate($dt, $en, $attrs);
			$i = $dt->Rows->size();
			return $i;
		}
		catch (\RuntimeException $ex)
		{
			throw $ex;
		}
	}
	public static function Retrieve_Entity_String_Paras ($en, $sql, $paras) // [Entity en, String sql, Paras paras]
	{
		$dt = null;
		switch ($en->getEnMap()->getEnDBUrl()->getDBUrlType()) {
			case $AppCenterDSN:
				$dt = DBAccess->RunSQLReturnTable($sql, $paras);
				break;
			default:
				throw new \RuntimeException("@没有设置DB类型�?");
		}
		if (($dt->Rows->size() == 0))
		{
			return 0;
		}
		$attrs = $en->getEnMap()->getAttrs();
		EntityDBAccess::fullDate($dt, $en, $attrs);
		$num = $dt->Rows->size();
		return $num;
	}
	public static function Retrieve_Entity_String ($en, $sql) // [Entity en, String sql]
	{
		try 
		{
			$dt = new DataTable();
			switch ($en->getEnMap()->getEnDBUrl()->getDBUrlType()) {
				case $AppCenterDSN:
					$dt = DBAccess->RunSQLReturnTable($sql);
					break;
				default:
					throw new \RuntimeException("@没有设置DB类型�?");
			}
			if (($dt->Rows->size() == 0))
			{
				return 0;
			}
			$attrs = $en->getEnMap()->getAttrs();
			EntityDBAccess::fullDate($dt, $en, $attrs);
			$i = $dt->Rows->size();
			return $i;
		}
		catch (\RuntimeException $ex)
		{
			throw $ex;
		}
	}
	protected static function fullDate ($dt, $en, $attrs) // [DataTable dt, Entity en, Attrs attrs]
	{
		foreach ($attrs as $attr) 		{
			$en->getRow()->SetValByKey($attr->getKey(), $dt->Rows->get(0)->getValue($attr->getKey()));
		}
	}
	public static function Retrieve_Entities_String ($ens, $sql) // [Entities ens, String sql]
	{
		try 
		{
			$dt = new DataTable();
			switch ($ens->getNewEntity()->getEnMap()->getEnDBUrl()->getDBUrlType()) {
				case $AppCenterDSN:
					$dt = DBAccess->RunSQLReturnTable($sql);
					break;
				default:
					throw new \RuntimeException("@没有设置DB类型�?");
			}
			if (($dt->Rows->size() == 0))
			{
				return 0;
			}
			$enMap = $ens->getNewEntity()->getEnMap();
			$attrs = $enMap->getAttrs();
			foreach ($dt->Rows as $dr) 			{
				$en = $ens->getNewEntity();
				foreach ($attrs as $attr) 				{
					$en->getRow()->SetValByKey($attr->getKey(), $dr->get($attr->getKey()));
				}
			/* match: Entity */
				$ens->AddEntity_Entity($en);
			}
			$i = $dt->Rows->size();
			return $i;
		}
		catch (\RuntimeException $ex)
		{
			throw new \RuntimeException(((("@在[" . $ens->getNewEntity()->getEnDesc()) . "]查询时出现错�?:") . $ex->getMessage()));
		}
	}
	public static function Retrieve_Entities_String_Paras_aString ($ens, $sql, $paras, $fullAttrs) // [Entities ens, String sql, Paras paras, String[] fullAttrs]
	{
		$dt = NULL;
		switch ($ens->getNewEntity()->getEnMap()->getEnDBUrl()->getDBUrlType()) {
			case $AppCenterDSN:
				$dt = DBAccess->RunSQLReturnTable($sql, $paras);
				break;
			default:
				throw new \RuntimeException("@没有设置DB类型�?");
		}
		if (($dt->Rows->size() == 0))
		{
			return 0;
		}
		QueryObject::InitEntitiesByDataTable($ens, $dt, $fullAttrs);
		$i = $dt->Rows->size();
		return $i;
	}
}
?>
